package com.arvato.minipro.service.user;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.user.CardHistoryDo;
import com.arvato.minipro.bean.bean_do.user.EcUserCardDo;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.user.CardHistoryVo;
import com.arvato.minipro.bean.bean_vo.user.EcUserCardVo;
import com.arvato.common.https.WxPublicAccountRequestUtil;
import com.arvato.minipro.controller.front.member.MemberController;
import com.arvato.minipro.mapper.user.CardHistoryMapper;
import com.arvato.minipro.mapper.user.EcUserCardMapper;


@Service
@Transactional
public class CardService {
	
	@Autowired
	private EcUserCardMapper ecUserCardMapper;
	
	@Autowired
	private CardHistoryMapper cardHistoryMapper;
	
	@Autowired
	private WxPublicAccountRequestUtil wxPublicAccountRequestUtil;
	
	/**
	 * 查询一个会员卡的详情
	 * @param cardDo
	 * @return
	 */
	public Result memberInfo(String userId){
		Result result = new Result(false);
		Query query = new Query();
		query.put("userId", userId);
		List<EcUserCardVo> cardList = listCardByQuery(query);
		if(cardList == null || cardList.size() == 0) {
			// 当前本地库存在此userId对应的code
			result.setCode(504);
			result.setMsg("当前数据库不存在此userId对应的code");
			return result;
		}
		return wxPublicAccountRequestUtil.getMemberInfo(MemberController.CARD_ID, cardList.get(0).getCode());
	}
	
	/**
	 * 更改会员卡积分详情
	 * @param userId
	 * @param addBonus
	 * @param recordBonus
	 * @return
	 */
	public Result updateBonus(String userId, int addBonus, String recordBonus) {
		// 先查出来当前用户之前的积分
		Result result = memberInfo(userId);
		if(result.getCode() != 200) {
			return result;
		}
		@SuppressWarnings("unchecked")
		Map<String, String> map = (Map<String, String>)result.getData();
		result.setResult(false);
		int oldbonus = Integer.valueOf(map.get("bonus"));
		int currbonus = addBonus + oldbonus;
		if(currbonus < 0) {
			result.setData("积分不足");
			return result;
		}
		Query query = new Query();
		query.put("userId", userId);
		List<EcUserCardVo> cardList = listCardByQuery(query);
		if(cardList != null && cardList.size() >= 1) {
			String cardId = cardList.get(0).getCardId();
			String wxCardId = cardList.get(0).getCardIdWx();
			String code = cardList.get(0).getCode();
			// 记录用户积分流水在本地
			CardHistoryDo cardHistoryDo = new CardHistoryDo();
			cardHistoryDo.setCardId(cardId);
			cardHistoryDo.setCurrValue(currbonus);
			cardHistoryDo.setPreValue(oldbonus);
			cardHistoryDo.setChangeValue(addBonus);
			cardHistoryDo.setDetail(recordBonus);
			cardHistoryDo.setCreateTime(new Date());
			cardHistoryMapper.insert(cardHistoryDo);
			return wxPublicAccountRequestUtil.updateBonus(wxCardId, code, currbonus, addBonus, recordBonus);
		}
		return result;
	}
	
	/**
	 * 插入一个会员卡关联
	 * @param cardDo
	 * @return
	 */
	public int insert(EcUserCardDo cardDo){
		int row = ecUserCardMapper.insert(cardDo);
		// 第一次领卡 会赠送100积分，将其记录下来
		if(row == 1) {
			CardHistoryDo cardHistoryDo = new CardHistoryDo();
			cardHistoryDo.setCardId(cardDo.getCardId());
			cardHistoryDo.setCurrValue(100);
			cardHistoryDo.setPreValue(0);
			cardHistoryDo.setChangeValue(100);
			cardHistoryDo.setDetail("开卡赠送100积分");
			cardHistoryDo.setCreateTime(new Date());
			cardHistoryMapper.insert(cardHistoryDo);
		}
		return row;
	}
	
	/**
	 * 删除
	 * @param cardId
	 * @return
	 */
	public int delete(String cardId) {
		int row = ecUserCardMapper.delete(cardId);
		return row;
	}

	/**
	 * 根据条件查询会员卡
	 * @param query
	 * @return
	 */
	public List<EcUserCardVo> listCardByQuery(Query query) {
		return ecUserCardMapper.listCardByQuery(query);
	}
	
	/**
	 * 查询会员卡的积分消费记录
	 * @param query
	 * @return
	 */
	public List<CardHistoryVo> listHistoryByQuery(Query query) {
		return cardHistoryMapper.listHistoryByQuery(query);
	}
	
	
}
